﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace WordSpider.Entities
{
    /// <summary>
    /// 单词实体类
    /// </summary>
    public class WordEntity
    {
        public string SoundMark { get; set; }
        public string English { get; set; }
        public string Chinese { get; set; }
        public DateTime CreateDate { get; set; }

        public List<WordGroupEntity> WordGroups { get; set; }
        public List<SentenceEntity> Sentences { get; set; }
        public List<SynonymEntity> Synonyms { get; set; }

        /// <summary>
        /// 从数据实体中获得实体对象
        /// </summary>
        /// <param name="tableEnglish"></param>
        /// <returns></returns>
        public static WordEntity FromTable(Table_English tableEnglish)
        {
            var xml = tableEnglish.CompleteData;
            var element = XElement.Load(new StringReader(xml));
            var soundmark = element.Element("SM").Value;
            var wordChinese = element.Element("C").Value;

            //词组
            var wordGroups = new List<WordGroupEntity>();
            var wordGroupElements = element.Element("WGS").Elements("WG");
            foreach (XElement xElement in wordGroupElements)
            {
                wordGroups.Add(new WordGroupEntity()
                                   {
                                       English = xElement.Element("E").Value,
                                       Chinese = xElement.Element("C").Value,
                                   });
            }

            //例句
            var sentences = new List<SentenceEntity>();
            var sentenceElements = element.Element("Sents").Elements("Sent");
            foreach (XElement xElement in sentenceElements)
            {
                sentences.Add(new SentenceEntity()
                                  {
                                      English = xElement.Element("E").Value,
                                      Chinese = xElement.Element("C").Value,
                                  });
            }

            //同义词
            var synonyms = new List<SynonymEntity>();
            var synonymsElements = element.Element("Syns").Elements("Syn");
            foreach (XElement xElement in synonymsElements)
            {
                synonyms.Add(new SynonymEntity()
                {
                    English = xElement.Element("E").Value,
                    Chinese = xElement.Element("C").Value,
                });
            }

            return new WordEntity()
                       {
                           English = tableEnglish.English,
                           SoundMark = soundmark,
                           Chinese = wordChinese,
                           CreateDate = tableEnglish.DownloadDate,

                           WordGroups = wordGroups,
                           Sentences = sentences,
                           Synonyms = synonyms,
                       };
        }



        /// <summary>
        /// 转换成xml字符串
        /// </summary>
        /// <returns></returns>
        public string ToXML()
        {
            var word = ToXElement();

            return word.ToString();
        }

        /// <summary>
        /// 转换成XElement
        /// </summary>
        /// <returns></returns>
        public XElement ToXElement()
        {
            //单词含义
            var word =
                new XElement("W",
                             new XElement("SM", SoundMark),
                             new XElement("E", English),
                             new XElement("C", Chinese));
            word.Add();

            //词组
            var wordGroup = new XElement("WGS");
            if (WordGroups != null)
            {
                WordGroups.ForEach(x => wordGroup.Add(new XElement("WG",
                                                                   new XElement("E", x.English),
                                                                   new XElement("C", x.Chinese))));
            }
            word.Add(wordGroup);

            //例句
            var sentences = new XElement("Sents");
            if (Sentences != null)
            {
                Sentences.ForEach(x => sentences.Add(new XElement("Sent",
                                                                  new XElement("E", x.English),
                                                                  new XElement("C", x.Chinese))));
            }
            word.Add(sentences);

            //同义词
            var synonyms = new XElement("Syns");
            if (Synonyms != null)
            {
                Synonyms.ForEach(x => synonyms.Add(new XElement("Syn",
                                                                new XElement("E", x.English),
                                                                new XElement("C", x.Chinese))));
            }
            word.Add(synonyms);
            return word;
        }

        /// <summary>
        /// 只有英文、音标、中文，无例句、词组、同义词
        /// </summary>
        /// <returns></returns>
        public XElement ToXElementSimple()
        {
            //单词含义
            var word =
                new XElement("W",
                             new XElement("SM", SoundMark),
                             new XElement("E", English),
                             new XElement("C", Chinese));

            //词组
            var wordGroup = new XElement("WGS");
            word.Add(wordGroup);

            //例句
            var sentences = new XElement("Sents");
            word.Add(sentences);

            //同义词
            var synonyms = new XElement("Syns");
            if (Synonyms != null)
            {
                Synonyms.ForEach(x => synonyms.Add(new XElement("Syn",
                                                                new XElement("E", x.English),
                                                                new XElement("C", x.Chinese))));
            }
            word.Add(synonyms);

            return word;
        }

        /// <summary>
        /// 灵活的转换
        /// </summary>
        /// <param name="isSynonyms">同义词</param>
        /// <param name="isSents">例句，带5个</param>
        /// <param name="isWordGroup">词组</param>
        /// <returns></returns>
        public XElement ToXElementFree(bool isSynonyms, bool isSents, bool isWordGroup, int sentenceCount)
        {
            //单词含义
            var word =
                new XElement("W",
                             new XElement("SM", SoundMark),
                             new XElement("E", English),
                             new XElement("C", Chinese));

            //词组
            var wordGroup = new XElement("WGS");
            if (isWordGroup)
            {
                if (WordGroups != null)
                {
                    WordGroups.ForEach(x => wordGroup.Add(new XElement("WG",
                                                                       new XElement("E", x.English),
                                                                       new XElement("C", x.Chinese))));
                }
            }
            word.Add(wordGroup);

            //例句
            var sentences = new XElement("Sents"); 
            if (isSents)
            {
                if (Sentences != null)
                {
                    Sentences.Take(sentenceCount).ToList()
                        .ForEach(x => sentences.Add(new XElement("Sent",
                                                                      new XElement("E", x.English),
                                                                      new XElement("C", x.Chinese))));
                }
            }
            word.Add(sentences);

            //同义词
            var synonyms = new XElement("Syns"); 
            if (isSynonyms)
            {
                if (Synonyms != null)
                {
                    Synonyms.ForEach(x => synonyms.Add(new XElement("Syn",
                                                                    new XElement("E", x.English),
                                                                    new XElement("C", x.Chinese))));
                }
            }
            word.Add(synonyms);

            return word;
        }

        /// <summary>
        /// 转换成受限制的XElement
        /// </summary>
        /// <returns></returns>
        public XElement ToXElement(int count)
        {
            //单词含义
            var word =
                new XElement("W",
                             new XElement("SM", SoundMark),
                             new XElement("E", English),
                             new XElement("C", Chinese));
            word.Add();

            //词组
            var wordGroup = new XElement("WGS");
            if (WordGroups != null)
            {
                WordGroups.Take(5).ToList().ForEach(x => wordGroup.Add(new XElement("WG",
                                                                   new XElement("E", x.English),
                                                                   new XElement("C", x.Chinese))));
            }
            word.Add(wordGroup);

            //例句
            var sentences = new XElement("Sents");
            if (Sentences != null)
            {
                Sentences.Take(5).ToList().ForEach(x => sentences.Add(new XElement("Sent",
                                                                  new XElement("E", x.English),
                                                                  new XElement("C", x.Chinese))));
            }
            word.Add(sentences);

            //同义词
            var synonyms = new XElement("Syns");
            if (Synonyms != null)
            {
                Synonyms.Take(5).ToList().ForEach(x => synonyms.Add(new XElement("Syn",
                                                                new XElement("E", x.English),
                                                                new XElement("C", x.Chinese))));
            }
            word.Add(synonyms);
            return word;
        }
    }
}
